package com.dingshi.security;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.dingshi.security.adapter.DispatchAdapter;
import com.dingshi.security.api.ApiResult;
import com.dingshi.security.api.ApiReturnResultListener;
import com.dingshi.security.api.RepairApi;
import com.dingshi.security.db.SettingLoader;
import com.dingshi.security.entity.Track;
import com.dingshi.security.util.Utils;
import com.dingshi.security.view.LoadDialog;

public class DispatchListActivity extends BaseActivity implements OnClickListener {
	protected static final int ID_LOADING = 2300;

	private Button btn_back;
	private TextView title_text;
	private Button btn_right;
	private ListView listView;
	protected ArrayList<Track> list;
	private DispatchAdapter adapter;
	private TextView tips;
	public int types;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dispatch_track);

		initTitle();
		initComponents();
		registerButton();

		listView.setCacheColorHint(Color.TRANSPARENT);
	}
	
	@Override
	protected void onStart() {
		getTrackList(SettingLoader.getGuardNum(this), SettingLoader
				.getLoginUserType(this));
		super.onStart();
	}

	private void getTrackList(String guardNum, String deprNum) {

		RepairApi api = new RepairApi(this);
		api.setReturnResultListener(new ApiReturnResultListener() {

			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(ID_LOADING);
				JSONObject jsonEntity = apiResult.getJsonEntity();
				Log.e("aa", jsonEntity.toString());
				if (jsonEntity != null) {
					// {"d":[{"__type":"DiaryReturn:r","c":"aaa","t":"01:59:05"}],"t":1}

					try {
						int type = jsonEntity.getInt("t");
						switch (type) {
						case 0:// 对象
							break;
						case 1:// 数组
							JSONArray array = jsonEntity.getJSONArray("d");
							if (array != null) {
								list = new ArrayList<Track>();
								Track track = null;
								Log.e("array", "array is " + array.length());
								for (int i = 0; i < array.length(); i++) {
									track = new Track();
									JSONObject opt = (JSONObject) array.opt(i);
									track.setOddNum(opt.getString("d"));
									track.setArea(opt.getString("a"));
									track.setName(opt.getString("n"));
									track.setStatus(opt.getString("s"));
									Log.e("status", "status is " + opt.getString("s"));
									track.setReportDate(opt.getString("t"));
									if (opt.has("tc")) {
										track.setFinishDate(opt.getString("tc"));
									}
									track.setDeviceName(opt.getString("e"));
									list.add(track);
								}
								Log.i("Track", "size is " + list.size());
								updateList();
							}
							break;
						case -1:// 错误
							dismissDialog(ID_LOADING);
							break;

						default:
							dismissDialog(ID_LOADING);
							break;
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public <T> void onReturnFailResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(ID_LOADING);
			}

			@Override
			public <T> void onReturnLoading(int requestCode) {
				showDialog(ID_LOADING);
			}
		});
		api.getRepairList(1, SettingLoader.getGuardNum(this), SettingLoader
				.getLoginUserType(this));

	}

	private void updateList() {
		ArrayList<Track> mList = handle(list);
		if (mList != null && mList.size() > 0) {
			tips.setVisibility(View.GONE);
			listView.setVisibility(View.VISIBLE);
			Log.e("list", "diary count is " + mList.size());
			adapter = new DispatchAdapter(DispatchListActivity.this, mList);
			listView.setAdapter(adapter);
		} else {
			tips.setVisibility(View.VISIBLE);
			listView.setVisibility(View.GONE);
		}
	}

	private ArrayList<Track> handle(ArrayList<Track> list) {
		ArrayList<Track> mList = new ArrayList<Track>();
		for (Track track : list) {
			Log.e("status", "status is " + track.getStatus());
			if ("2".equals(track.getStatus())) {
				mList.add(track);
			}
		}
		return mList;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ID_LOADING:
			LoadDialog dialog = new LoadDialog(this, getResources().getString(
					R.string.login_loading));
			return dialog;

		default:
			break;
		}
		return super.onCreateDialog(id);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
	}

	private void initTitle() {
		btn_back = (Button) findViewById(R.id.btn_back);
		btn_right = (Button) findViewById(R.id.btn_refresh);
		btn_right.setVisibility(View.VISIBLE);

		title_text = (TextView) findViewById(R.id.title_text);
		title_text.setText("派工接收列表");

	}

	private void registerButton() {
		btn_back.setOnClickListener(this);
		btn_right.setOnClickListener(this);
	}

	private void initComponents() {
		tips = (TextView) findViewById(R.id.tips);
		listView = (ListView) findViewById(R.id.listView1);
		listView.setDividerHeight(0);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			super.onBackPressed();
			break;
		case R.id.btn_refresh:
			getTrackList(SettingLoader.getGuardNum(this), SettingLoader
					.getLoginUserType(this));
			break;
		default:
			break;
		}
	}
}
